package com.xj.edu.hotel.controller;

import com.xj.edu.hotel.domain.HisOrder;
import com.xj.edu.hotel.domain.Order;
import com.xj.edu.hotel.domain.Room;
import com.xj.edu.hotel.service.OrderService;
import com.xj.edu.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private RoomService roomService;
    /**
     * 添加订单，无需添加，由前端预定
     */

    /**
     * 修改订单
     */
    @RequestMapping("editOrder")
    public String editOrder(String orderId,String orderStatus, Date orderInTime){
        System.out.println(orderId);
        Order order = new Order();
        order.setOrderId(orderId);
        order.setOrderStatus(orderStatus);
        order.setOrderInTime(orderInTime);
        orderService.editOrder(order);
        return "redirect:/order/findOrder";
    }
    /**
     * 删除订单
     */
    @RequestMapping("delOrder")
    public String delOrder(String id){
       //先找到这个订单
//       Order order = orderService.findByIdOrder(id);
//       //查找这个订单的hotelId和房间id
//       String hotelId = order.getHotelId();
//       Integer roomId =order.getRoomNumber();
//       Room room = roomService.findRoomByHotelIdAndRoomId(hotelId,roomId);
//       //修改房间状态
//       room.setRoomStatus(0);
//       roomService.updateRoomStatus(room);
       orderService.delOrder(id);
       return "redirect:/order/findOrder";
    }
    /**
     * 查询订单
     */
    @RequestMapping("findOrder")
    public String findOrder(Model model){
        List<Order> orders = orderService.findAllOrder();
        model.addAttribute("orders",orders);
        return "order";
    }
    /**
     * 使用id查找订单，用于修改时订单回显
     */
    @RequestMapping("findById")
    public String findById(String id,Model model){
        Order order = orderService.findByIdOrder(id);
        model.addAttribute("order",order);
        return "editOrder";
    }

    /**
     *
     * 查看历史订单
     */
    @RequestMapping("hisOrder")
    public String hisOrder(Model model){
        List<HisOrder> hisOrders = orderService.findAllHisOoder();
        model.addAttribute("hisOrder",hisOrders);
        return "BackHisOrder";
    }

}
